package JDBC;

import java.sql.*;

/**
 * 这是一个JDBC工具类
 * @Author Alex
 */
public class JDBCUtils {

    /**
     * 返回一个连接器
     * 此处连接的是mysql的scoredatabase数据库
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public static Connection get() throws SQLException, ClassNotFoundException {
        //1.注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.返回连接
        String url = "jdbc:mysql://localhost:3306/scoredatabase?characterEncoding=utf8";
        return DriverManager.getConnection(url,"root","root");
    }

    /**
     * 关闭资源
     * @param con
     * @param ps
     * @param rs
     */
    public static void close(Connection con, PreparedStatement ps, ResultSet rs){
        if (con!=null){
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                con=null;
            }
        }
        if (ps!=null){
            try {
                ps.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                ps=null;
            }
        }
        if (rs!=null){
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                rs=null;
            }
        }
    }

}
